Unify the metrics implementation between low-level and high-level API.#26158
Unify the metrics implementation between low-level and high-level API.#26158qingqing01 merged 21 commits intoPaddlePaddle:developfrom
Conversation
|
Thanks for your contribution! |
|
✅ This PR's description meets the template requirements! |
12bab82 to
3659013
Compare
python/paddle/metric/metrics.py
Outdated
test=develop
3659013 to
6b9e9d7
Compare
test=develop
python/paddle/incubate/hapi/model.py
Outdated
| and test mode. | ||
| loss_function (Loss|callable function|None): Loss function can | ||
| loss (Loss|callable function|None): Loss function can | ||
| be a `fluid.dygraph.Layer` instance or any callable function |
There was a problem hiding this comment.
fluid.dygraph.Layer => paddle.nn.Layer ,下面用到fluid.dygraph.Layer的地方同样都需要修改一下
python/paddle/incubate/hapi/model.py
Outdated
| print(result) | ||
|
|
||
| # imperative mode | ||
| fluid.enable_dygraph() |
There was a problem hiding this comment.
fluid.enable_dygraph => paddle.disable_static
python/paddle/metric/metrics.py
Outdated
|
|
||
| Args: | ||
| name (str, optional): String name of the metric instance. | ||
| Default is `precision`. |
python/paddle/metric/metrics.py
Outdated
|
|
||
| Args: | ||
| name (str, optional): String name of the metric instance. Default | ||
| is `acc`. |
| name (str, optional): String name of the metric instance. Default | ||
| is `acc`. | ||
| curve (str): Specifies the mode of the curve to be computed, | ||
| 'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'. |
There was a problem hiding this comment.
Maybe add doc for num_thresholds later
python/paddle/metric/metrics.py
Outdated
| .. code-block:: python | ||
| def compute(pred, label): | ||
| # sort prediction and slice the top-5 scores | ||
| pred = fluid.layers.argsort(pred, descending=True)[1][:, :5] |
python/paddle/metric/metrics.py
Outdated
| pred = fluid.layers.argsort(pred, descending=True)[1][:, :5] | ||
| # calculate whether the predictions are correct | ||
| correct = pred == label | ||
| return fluid.layers.cast(correct, dtype='float32') |
python/paddle/metric/metrics.py
Outdated
| class Accuracy(Metric): | ||
| """ | ||
| Encapsulates accuracy metric logic. | ||
| def __init__(self, topk=(1, ), name='acc', *args, **kwargs): |
There was a problem hiding this comment.
Removed. Thanks!
python/paddle/metric/metrics.py
Outdated
| import paddle | ||
|
|
||
| paddle.disable_static() | ||
| x = paddle.to_variable(np.array([ |
python/paddle/metric/metrics.py
Outdated
| [0.1, 0.4, 0.3, 0.2], | ||
| [0.1, 0.2, 0.4, 0.3], | ||
| [0.1, 0.2, 0.3, 0.4]])) | ||
| y = paddle.to_variable(np.array([[0], [1], [2], [3]])) |
python/paddle/metric/metrics.py
Outdated
| .. code-block:: python | ||
|
|
||
| import paddle | ||
| import paddle.fluid as fluid |
python/paddle/metric/metrics.py
Outdated
| train_dataset = hapi.datasets.MNIST(mode='train') | ||
|
|
||
| model = hapi.Model(hapi.vision.LeNet(classifier_activation=None)) | ||
| optim = fluid.optimizer.Adam( |
test=develop
… hapi_metrics test=develop
test=develop
test=develop
test=develop
|
|
||
|
|
||
|
|
||
| from ..fluid.metrics import Accuracy, Auc, ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance, \ |
There was a problem hiding this comment.
paddle/fluid/metrics.py 里的API计划怎么处理?
还有ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance在重构后的paddle.metric下没有对应的API。
There was a problem hiding this comment.
相关被使用统计在agroup中。 DetectionMAP、EditDistance建议先废弃,相关套件已在模型里重新实现。Precision/Recall当前没有被任何模型使用到,当前fluid下面的实现还太简单。 会上讨论,本次PR先以统一位置为主,后续版本还需要增强相关功能,包括增加ChunkEvaluator,以及改进功能。
There was a problem hiding this comment.
计划废弃的API都添加一下deprecated decorator吧。
| from . import metrics | ||
|
|
||
| from ..fluid.layers.metric_op import accuracy, auc | ||
| from ..fluid.layers.nn import chunk_eval, cos_sim, mean_iou |
There was a problem hiding this comment.
paddle.metric下的class形式API,跟小写的调用op的API的关系,是否讨论过啊?
There was a problem hiding this comment.
class形式: 提供batch累积功能。一般用于高层API。底层API也可以使用。 参考内部wiki记录。
function(op形式): 保留了fluid下面的api,一般用于底层API。
- Accuracy: 只支持当前batch的统计,需用户做batch累积计算。底层API可以使用。
- auc: 可计算batch和累积batch的auc。 PaddleRec中使用。
python/paddle/metric/metrics.py
Outdated
| model.prepare( | ||
| optim, | ||
| loss=nn.BCELoss(), | ||
| metrics=paddle.metric.Recall()) |
There was a problem hiding this comment.
这里给一个可以同时计算precison和recall的例子?
通常情况下会同时算这两个指标的。
There was a problem hiding this comment.
@jzhang533 Precision和Recall的功能会上讨论后续的版本还需增强,见上面回复,本次先以统一为主。所以后续可以增强下。
There was a problem hiding this comment.
我看model.prepare的metrics是可以接受list的,这里改一下是否就可以?(先不考虑运行效率问题)
| "Auc.update", | ||
| "Auc.accumulate", | ||
| "Auc.name", | ||
| "Auc.compute", |
There was a problem hiding this comment.
同意这次对于白名单的改动。因为在class Auc等的地方已经有了示例代码。
test=develop
jzhang533
left a comment
There was a problem hiding this comment.
lgtm
will have follow up pr to deprecate fluid apis.
0fb112c
|
Fix sample code about Adam, since #26288 is merged. @XiaoguangHu01 @raindrops2sea @jzhang533 @saxon-zh Please help to review again. |
PR types
Function optimization
PR changes
APIs
Describe
add_metric_opin Metric tocompute.update()andaccumulate()ofAccuracy.Accuracy.compute,update,reset..ModelAPI for Metricspython/paddle/tests